﻿using Domain.Model;
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    /// <summary>
    /// 信息管理
    /// </summary>
    public class InfoController : LoginedController
    {
        //
        // GET: /Information/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult NewsPage(int pageIndex,int pageSize,string keyword)
        {
            var where = Where.True<News>();
            if (keyword != null)
            {
                where = where.And(i => i.Title.Contains(keyword) );
            }
            var page = DbContext.News.Where(where).OrderByDescending(i => i.CreateTime).ToPage(pageIndex, pageSize);
            return this.PageResult(page.TotalCount, this.ViewHtml(page));
        }
        //添加新闻
        public ActionResult NewsCreate()
        {
            return View();
        }
        [HttpPost]
        public ActionResult NewsCreate(News model)
        {
            DbContext.News.Add(model);
            DbContext.SaveChanges();
            return Json(new { state = true, value = "index" });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult NewsDel(Guid id)
        {
           var model=  DbContext.News.Find(id);
           DbContext.News.Remove(model);
           DbContext.SaveChanges();
           
            return Json(new { state = true, value = "删除成功" });
        }

        /// <summary>
        /// 修改需要状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangeNewsValid(Guid id, bool value)
        {
            var model = this.DbContext.News.Find(id);
            model.IsValid = value;
            this.DbContext.SaveChanges();
            return Json(new { state = true });
        }

    }
}
